//
//  CaseHeadView.m
//  FastDoctor
//
//  Created by 程建良 on 16/7/8.
//  Copyright © 2016年 sc. All rights reserved.
//

#import "CaseHeadView.h"
#define kFilePath(fileName) ([NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:fileName])
@interface CaseHeadView ()
@property (nonatomic,strong)UIImageView *headImageView;
@property (nonatomic,strong)UILabel *nameLabel;
@property (nonatomic,strong)UIImageView *sexImageView;
@property (nonatomic,strong)UIImageView *idImageView;
@property (nonatomic,strong)UIImageView *phoneImageView;
@property (nonatomic,strong)UIImageView *addrImageView;
@property (nonatomic,strong)UILabel *sexLabel;
@property (nonatomic,strong)UILabel *idLabel;
@property (nonatomic,strong)UILabel *numberLabel;
@property (nonatomic,strong)UILabel *addrLabel;
@property (nonatomic,strong)NSDictionary *dic;
@property (nonatomic,strong)UIView *lineView;
@end
@implementation CaseHeadView
-(instancetype)init{
    if (self = [super init]) {
        [self setupUI];
    }
    return self;
}
#pragma mark - UI
-(void)setupUI{
    [self setupHeadImageView];
    self.dic = [NSKeyedUnarchiver unarchiveObjectWithFile:kFilePath(@"user.data")];
    self.nameLabel = [[UILabel alloc]init];
    [self setLabel:self.nameLabel WithStr:self.dic[@"true_name"]];
    self.sexImageView = [[UIImageView alloc]init];
    [self setImageView:self.sexImageView andImgStr:@"0"];
    [self setupSexLabel];
    self.idImageView = [[UIImageView alloc]init];
    [self setImageView:self.idImageView andImgStr:@"idCard"];
    self.idLabel = [[UILabel alloc]init];
    [self setLabel:self.idLabel WithStr:self.dic[@"card_number"]];
    self.phoneImageView = [[UIImageView alloc]init];
    [self setImageView:self.phoneImageView andImgStr:@"phone"];
    self.numberLabel = [[UILabel alloc]init];;
    [self setLabel:self.numberLabel WithStr:self.dic[@"mobile_number"]];
    self.addrImageView = [[UIImageView alloc]init];
    [self setImageView:self.addrImageView andImgStr:@"illness_rb_img_sel.png"];
    [self setupAddressLabel];
    self.lineView = [[UIView alloc]init];
    self.lineView.backgroundColor = [UIColor grayColor];
    [self addSubview:self.lineView];
}
-(void)setupAddressLabel{
    self.addrLabel = [[UILabel alloc]init];
    if ([[NSUserDefaults standardUserDefaults]objectForKey:@"cicty"]) {
        self.addrLabel.text = [[NSUserDefaults standardUserDefaults]objectForKey:@"cicty"];
    }else{
        self.addrLabel.text = self.dic[@"address"];
    }
    [self.addrLabel sizeToFit];
    [self addSubview:self.addrLabel];
}
-(void)setLabel:(UILabel *)label WithStr:(NSString *)str{
    label.text = str;
    if (str == self.dic[@"true_name"]) {
        label.font = [UIFont systemFontOfSize:18];
    }
    [label sizeToFit];
    [self addSubview:label];
}
-(void)setImageView:(UIImageView *)imageView andImgStr:(NSString *)imgStr{
    imageView.image = [UIImage imageNamed:imgStr];
    [imageView sizeToFit];
    [self addSubview:imageView];
}
-(void)setupSexLabel{
    self.sexLabel = [[UILabel alloc]init];
    NSString *str1 =[self.dic[@"gender"] integerValue] ? @"男" : @"女";
    NSString *str2 = [NSString stringWithFormat:@"%ld",[self.dic[@"age"] integerValue]];
    self.sexLabel.text = [NSString stringWithFormat:@"%@  %@",str1,str2];
    [self.sexLabel sizeToFit];
    [self addSubview:self.sexLabel];
}
-(void)setupHeadImageView{
    UIImage *image = [NSKeyedUnarchiver unarchiveObjectWithFile:kFilePath(@"image.data")];
    self.headImageView = [[UIImageView alloc]initWithImage:image];
    self.headImageView.layer.cornerRadius = 40;
    self.headImageView.clipsToBounds = YES;
    [self addSubview:self.headImageView];
}
#pragma mark - masonry
-(void)layoutSubviews{
    [super layoutSubviews];
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(5);
        make.top.offset(20);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(80);
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.headImageView.mas_bottom).offset(20);
        make.left.offset(15);
    }];
    [self.sexImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.headImageView.mas_top);
        make.left.mas_equalTo(self.headImageView.mas_right).offset(20);
    }];

    [self.sexLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.headImageView.mas_top);
        make.left.mas_equalTo(self.sexImageView.mas_right).offset(20);
        make.height.mas_equalTo(self.sexImageView);
    }];
    [self.idImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.sexImageView.mas_bottom).offset(10);
        make.leading.mas_equalTo(self.sexImageView.mas_leading);
    }];
    [self.idLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.idImageView.mas_top);
        make.left.mas_equalTo(self.idImageView.mas_right).offset(20);
        make.height.mas_equalTo(self.idImageView);
    }];
    [self.phoneImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.idImageView.mas_bottom).offset(10);
        make.left.mas_equalTo(self.sexImageView.mas_left);
    }];
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.phoneImageView.mas_top);
        make.left.mas_equalTo(self.phoneImageView.mas_right).offset(20);
        make.height.mas_equalTo(self.phoneImageView);
    }];
    [self.addrImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.phoneImageView.mas_bottom).offset(10);
        make.left.mas_equalTo(self.sexImageView.mas_left);
    }];
    [self.addrLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.addrImageView.mas_top);
        make.left.mas_equalTo(self.addrImageView.mas_right).offset(20);
        make.height.mas_equalTo(self.addrImageView);
    }];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.right.offset(0);
        make.height.mas_equalTo(1);
        make.bottom.offset(-2);
    }];
}
@end
